<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Order\PaySuccess;
use App\Admin\Repositories\Order;
use App\Enums\OrderPay;
use App\Enums\PayMethod;
use App\Models\Project;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class OrderController extends AdminController
{
    public bool $create = false;
    public bool $edit = false;
    public bool $delete = false;
    public bool $disable_actions = true;

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Order(), function (Grid $grid) {
            $grid->model()->with(['user', 'project', 'material'])->orderByDesc('id');
            $grid->column('id');
            $grid->column('user.phone', trans('order.fields.user_id'));
            $grid->column('project.title', trans('order.fields.project_id'));
            $grid->column('material.name', trans('order.fields.material_id'));
            $grid->column('pay_method')->display(function ($value) {
                return PayMethod::getDescription($value);
            });
            $grid->column('money');
            $grid->column('order_no');
            $grid->column('trade_no');
            $grid->column('pay')->display(function ($value) {
                return OrderPay::getDescription($value);
            });
            $grid->column('pay_time');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('user.phone', trans('order.fields.user_id'));
                $filter->equal('order_no');
                $filter->equal('trade_no');
                $filter->between('pay_time')->datetime();
                $filter->equal('pay')->select(OrderPay::asSelectArray());
                $filter->equal('project_id')->select(Project::query()->pluck('title', 'id'));
            });
            $this->gridCommon($grid);
            $grid->export();
            $grid->export()->rows(function ($rows) {
                foreach ($rows as &$row) {
                    $row['pay_method'] = PayMethod::getDescription($row['pay_method']);
                    $row['pay'] = OrderPay::getDescription($row['pay']);
                }
                return $rows;
            });
//            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                if (config('app.env') == 'local') {
//                    $actions->append(new PaySuccess());
//                }
//            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('project_id');
            $show->field('material_id');
            $show->field('pay_method');
            $show->field('money');
            $show->field('order_no');
            $show->field('trade_no');
            $show->field('pay');
            $show->field('pay_time');
            $show->field('project_info');
            $show->field('pay_param');
            $show->field('pay_response');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

}
